package com.soft.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.soft.entity.TeamMember;

@Mapper
public interface TeamMemberMapper extends BaseMapper<TeamMember> {
    
    @Select("SELECT tm.*, u.username as userName, u.email as userEmail FROM team_member tm LEFT JOIN user u ON tm.user_id = u.user_id WHERE tm.team_id = #{teamId} AND tm.status = 'active'")
    List<TeamMember> selectByTeamId(Integer teamId);
    
    @Select("SELECT * FROM team_member WHERE team_id = #{teamId} AND user_id = #{userId}")
    TeamMember selectByTeamAndUser(Integer teamId, Integer userId);
    
    @Update("UPDATE team_member SET team_role = #{newRole} WHERE team_id = #{teamId} AND user_id = #{userId} AND status = 'active'")
    int updateRole(Integer teamId, Integer userId, String newRole);
    
    @Select("SELECT COUNT(*) FROM team_member WHERE team_id = #{teamId} AND user_id = #{userId} AND status = 'active'")
     int countActiveMembers(Integer teamId, Integer userId);
    
    @Select("SELECT COUNT(*) FROM team_member WHERE team_id = #{teamId} AND status = 'active' AND team_role = 'leader'")
    int countActiveLeaders(Integer teamId);
    
    @Update("UPDATE team_member SET status = #{status} WHERE team_id = #{teamId} AND user_id = #{userId}")
    int updateStatus(Integer teamId, Integer userId, String status);
    
    @Update("DELETE FROM team_member WHERE team_id = #{teamId} AND user_id = #{userId}")
    int deleteMember(Integer teamId, Integer userId);
    
    @Update("DELETE FROM team_member WHERE team_id = #{teamId}")
    int deleteByTeamId(Integer teamId);
}
